package com.example.noteback.permission.aop;

import com.example.noteback.permission.anno.Permissions;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Aspect
@Component
public class PermissionsValidation {

    //切入点
    @Pointcut("@annotation(com.example.noteback.permission.anno.Permissions)")
    public void permissionsPoint() {

    }

    @Around("permissionsPoint()")
    public Object hasPermissions(ProceedingJoinPoint joinPoint) throws Throwable {
        //方法签名
        MethodSignature methodSignature=(MethodSignature) joinPoint.getSignature();
        //获取方法
        Method aimMethod= methodSignature.getMethod();
        //获取 @Permissions 的值
        String permissions=aimMethod.getAnnotation(Permissions.class).value();
        //权限集合
        List<String> permissionsList=new ArrayList<>();
        permissionsList.add("/permissions/getUser");
        Object proceed=null;
        if(permissionsList.contains(permissions)){
            proceed=joinPoint.proceed();
        }else {
            proceed="没有权限";
        }
        return proceed;
    }
}
